<?php
class Config implements interface_config {
    private $filename;
    private $data;
    private $section;

    public function __construct($filename = null, $section = null, $data = array()){
        $this->filename = $filename;
        $this->section = $section;
        $this->data = $data;
    }

    public function set_filename($filename) {
        $this->filename = $filename;
        return $this;
    }

    public function set_section($section) {
        $this->section = $section;
        return $this;
    }

    public function set_all($data) {
        if(isset($this->section)){
            $this->data[$this->section] = $data;
        } else {
            $this->data = $data;
        }
    }

    public function get_all() {
        if(isset($this->section)){
            if(isset($this->data[$this->section])){
                return $this->data[$this->section];
            }
            return array();
        }
        return $this->data;
    }

    public function get($key) {
        if(is_null($this->section)){
            return (isset($this->data[$key]) ? $this->data[$key] : null);
        }
        return (isset($this->data[$this->section][$key]) ? $this->data[$this->section][$key] : null);
    }

    public function set($key, $value) {
        if(is_null($this->section)){
            $this->data[$key] = $value;
            return $this;
        }

        $this->data[$this->section][$key] = $value;
        return $this;
    }

    public function has($key) {
        if(is_null($this->section)){
            return isset($this->data[$key]);
        }
        return isset($this->data[$this->section][$key]);
    }

    public function parse_ini_file_quotes_safe($f) {
        // http://php.net/manual/en/function.parse-ini-file.php#57075

        $r=null;
        $sec=null;
        $f=@file($f);
        for ($i=0;$i<@count($f);$i++)
        {
            $newsec=0;
            $w=@trim($f[$i]);
            if ($w)
            {
                if ((!$r) or ($sec))
                {
                    if ((@substr($w,0,1)=="[") and (@substr($w,-1,1))=="]") {$sec=@substr($w,1,@strlen($w)-2);$newsec=1;}
                }
                if (!$newsec)
                {
                    $w=@explode("=",$w);$k=@trim($w[0]);unset($w[0]); $v=@trim(@implode("=",$w));
                    if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\"")) {$v=@substr($v,1,@strlen($v)-2);}
                    if ($sec) {$r[$sec][$k]=$v;} else {$r[$k]=$v;}
                }
            }
        }
        return $r;
    }



    public function parse(){
        if(!is_file($this->filename)){
            return false;
        }

        if(function_exists('parse_ini_file')) {
            $data = parse_ini_file($this->filename, true);
        } else {
            $data = $this->parse_ini_file_quotes_safe($this->filename);
        }

        if ($data){
            $this->data = array_merge($this->data, $data); 		
            return true;
        }
    }

    public function save($filename = null){

        if (!is_null($filename) && is_file($filename)) { 
            $this->filename = $filename;
        }

        if (!is_writable(dirname($this->filename))){
            return false;
        }

        $f = fopen($this->filename, "w");

        if(!$f){
            return false;
        }

        $st="";
        foreach(array_keys($this->data) as $sn){
            $st.="[".$sn."]"."\n";
            foreach($this->data["$sn"] as $key => $value){
                $st.="$key = $value\n";
            }
        }

        fwrite($f,$st);
        fclose($f);

        return true;
    }

}
?>
